/*
 * @Description: 上传SIP
 * @Author: Rfan
 * @Date: 2023-01-16 10:15:19
 * @LastEditTime: 2023-02-07 17:05:50
 */

import { useEffect, useState } from 'react';
import { Button, message, Modal, Space } from 'antd';
import classnames from 'classnames/bind';
import { saveTemplateRelation } from '@/api/accession';
import { useBatchManageContext } from '../../../contexts/BatchManageContext';
import UploadPane from './components/UploadPane';
import TransferTemplate from '@/components/TransferTemplate';
import { EnumTemplateTypes } from '@/view/system/TemplateManage/dictionaries';
import type { FC } from 'react';
import type { InputStatus } from 'antd/es/_util/statusUtils';
import type { ICommonDialog } from '@/interfaces/global';
import styles from '../../../styles/uploadSipDialog.module.scss';

const cx = classnames.bind(styles);

interface UploadOriginDialogProps extends ICommonDialog {}

const UploadSipDialog: FC<UploadOriginDialogProps> = (props) => {
  const { visible, onClose } = props;
  const { activeBatch } = useBatchManageContext();
  console.log('UploadSipDialog');
  // 选中的模板
  const [selectTemplate, setSelectTemplate] = useState(null);
  // 模板输入框状态
  const [templateState, setTemplateState] = useState<InputStatus>('');

  useEffect(() => {
    if (visible) {
      //
    }
  }, [visible]);

  const onCancel = () => {
    onClose();
  };

  /**
   * @description 保存批次模板关系，成功后关闭弹窗
   */
  const saveTempRelation = () => {
    if (selectTemplate) {
      saveTemplateRelation({
        batchId: activeBatch.id,
        templateId: selectTemplate
      }).then(() => {
        onCancel();
      });
    } else {
      setTemplateState('error');
      message.error('请选择模板');
    }
  };

  return (
    <Modal
      title="SIP管理"
      visible={visible}
      className={cx('upload-sip-dialog')}
      width={600}
      maskClosable={false}
      destroyOnClose
      onCancel={onCancel}
      footer={[
        <Button key="close" onClick={onCancel}>
          关闭
        </Button>,
        <Button key="confirm" onClick={saveTempRelation}>
          确认
        </Button>
      ]}
    >
      <p>
        当前批次档案为：全宗{activeBatch.fonds?.fondName} - 档案类型
        {activeBatch.archiveType?.atName}
      </p>
      <Space className={cx('template-wrap')}>
        <h4>请选择模板</h4>
        <TransferTemplate
          tplType={EnumTemplateTypes.SipImport}
          value={selectTemplate}
          templateState={templateState}
          onChange={setSelectTemplate}
        />
      </Space>
      <UploadPane
        batchId={activeBatch.id}
        atId={activeBatch.archiveTypeId}
        rule={activeBatch.attOriginal}
      />
    </Modal>
  );
};

export default UploadSipDialog;
